﻿<ResourceDictionary xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:ui="clr-namespace:Bonobo.Irc.Client.UI"
    xmlns:data="clr-namespace:Bonobo.Irc.Client.UI.Data"    
    xmlns:globalization="clr-namespace:System.Globalization;assembly=mscorlib"    
    xmlns:scm="clr-namespace:System.ComponentModel;assembly=WindowsBase"   
    x:Class="Bonobo.Irc.Client.UI.Data.IrcServerConversationTemplate">

    <DataTemplate x:Key="IrcServerConversationContentTemplate"> 
        <Grid>
            <Grid.Resources>
                <data:IPAddressConverter x:Key="IPAddressConverter" />
                <data:FormattingConverter x:Key="Formatter" />
                <data:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
                <data:MessageConverter x:Key="MessageConverter" />
                <data:MessageTypeToBrushConverter x:Key="MessageTypeToBrushConverter" />
            </Grid.Resources>
            <Grid.CommandBindings>
                <CommandBinding Command="{x:Static ui:IrcCommands.Login}" 
                                CanExecute="LoginCanExecute"
                                Executed="LoginExecuted" />
                <CommandBinding Command="{x:Static ui:IrcCommands.List}" 
                                Executed="ListExecuted" />
                <CommandBinding Command="{x:Static ui:IrcCommands.JoinChannel}" 
                                Executed="JoinChannelExecuted" />
                <CommandBinding Command="{x:Static ui:IrcCommands.AddChannel}" 
                                Executed="AddChannelExecuted" />
            </Grid.CommandBindings>
            <Grid>
                <Grid.Style>
                    <Style TargetType="{x:Type Grid}">
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Session.State}" Value="Closed">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Session.State}" Value="ServerError">
                                <Setter Property="Visibility" Value="Collapsed" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition />
                    <ColumnDefinition Width="200px" />
                </Grid.ColumnDefinitions>
                <ListBox Name="lvItems"
                         BorderThickness="0"
                         ItemsSource="{Binding Messages}"
                         SelectionMode="Single"
                         ScrollViewer.HorizontalScrollBarVisibility="Disabled"
                         Grid.Column="0"
                         Padding="5">
                    <ListBox.ItemTemplate>
                        <DataTemplate>
                            <DockPanel>
                                <TextBlock Text="{Binding TimeStamp, 
                                                StringFormat={}{0:g},
                                                ConverterCulture={x:Static globalization:CultureInfo.CurrentCulture}}"
                                           Foreground="Gray"
                                           TextWrapping="Wrap"
                                           Margin="0 2px 10px 2px" />
                                <TextBlock TextWrapping="Wrap"
                                           DockPanel.Dock="Right"
                                           Margin="0 2px 0 2px"
                                           Foreground="{Binding Type, Converter={StaticResource MessageTypeToBrushConverter}}"
                                           Text="{Binding Converter={StaticResource MessageConverter}}">                                    
                                    <TextBlock Margin="5px 0 0 0" Visibility="{Binding IsLoginError, Converter={StaticResource BoolToVisibilityConverter}}">
                                        (<Hyperlink Command="{x:Static ui:IrcCommands.Disconnect}">  
                                            Click here to relog                               
                                        </Hyperlink>)
                                    </TextBlock>  
                                 </TextBlock>
                            </DockPanel>
                        </DataTemplate>
                    </ListBox.ItemTemplate>
                </ListBox>
                <Grid Grid.Column="1">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition />
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="54px" />
                        <RowDefinition />
                        <RowDefinition Height="54px" />
                        <RowDefinition Height="27px" />
                    </Grid.RowDefinitions>
                    <StackPanel Grid.Row="0">
                        <TextBlock Padding="2"
                                   Foreground="Gray"
                                   FontWeight="Bold"
                                   Background="White">
                        Channels
                        </TextBlock>
                        <TextBox TextChanged="FilterTextBox_TextChanged"
                                 Margin="2px" />
                    </StackPanel>

                    <ListBox Grid.Row="1"
                             Name="lvChannels"
                             BorderThickness="0"
                             ItemsSource="{Binding Channels}"
                             SelectionMode="Multiple">
                        <ListBox.ItemTemplate>
                            <DataTemplate>
                                <TextBlock Text="{Binding Name }">
                                    <TextBlock.ToolTip>
                                        <ToolTip Content="{Binding Topic}">
                                            <ToolTip.Style>
                                                <Style TargetType="ToolTip">
                                                    <Style.Triggers>
                                                        <DataTrigger Binding="{Binding Topic}" Value="">
                                                            <Setter Property="Visibility" Value="Collapsed" />
                                                        </DataTrigger>
                                                    </Style.Triggers>
                                                </Style>
                                            </ToolTip.Style>
                                        </ToolTip>                                        
                                    </TextBlock.ToolTip>
                                </TextBlock>
                            </DataTemplate>
                        </ListBox.ItemTemplate>
                    </ListBox>
                    <StackPanel Grid.Row="2">
                        <Button Margin="2"
                                Command="{x:Static ui:IrcCommands.List}"
                                CommandParameter="{Binding}">_List Channels</Button>
                        <Button Margin="2"
                                Command="{x:Static ui:IrcCommands.JoinChannel}"
                                CommandParameter="{Binding}">_Join</Button>
                    </StackPanel>
                    <DockPanel Grid.Row="3">
                        <TextBox Width="120px"
                                 Margin="2"
                                 MaxLength="100"
                                 Name="txtChannel"/>
                        <Button DockPanel.Dock="Right"
                                Margin="2"
                                Command="{x:Static ui:IrcCommands.AddChannel}"
                                CommandParameter="{Binding}">_Add</Button>
                    </DockPanel>
                </Grid>
            </Grid>


            <Grid FocusManager.FocusedElement="{Binding ElementName=txtServerAddress}">
                <Grid.Style>
                    <Style TargetType="{x:Type Grid}">
                        <Setter Property="Visibility" Value="Collapsed" />
                        <Style.Triggers>
                            <DataTrigger Binding="{Binding Session.State}" Value="Closed">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                            <DataTrigger Binding="{Binding Session.State}" Value="ServerError">
                                <Setter Property="Visibility" Value="Visible" />
                            </DataTrigger>
                        </Style.Triggers>
                    </Style>
                </Grid.Style>
                <Grid.Resources>
                    <DataTemplate DataType="{x:Type ValidationError}">
                        <TextBlock FontStyle="Italic" 
                                           Foreground="Red"          
                                           HorizontalAlignment="Left"
                                           Margin="4"
                                           Text="{Binding Path=ErrorContent}" />
                    </DataTemplate>
                </Grid.Resources>
                <Grid.RowDefinitions>
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="27" />
                    <RowDefinition Height="150" />
                </Grid.RowDefinitions>
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="15" />
                    <ColumnDefinition Width="150" />
                    <ColumnDefinition Width="200" />
                    <ColumnDefinition />
                </Grid.ColumnDefinitions>
                <Label Grid.Column="1" 
                               Grid.Row="1">Server Address</Label>
                <TextBox Grid.Column="2" 
                                 Grid.Row="1" 
                                 x:Name="txtServerAddress"
                                 Margin="2">
                    <Binding Path="Session.ConnectionInfo.Address" Converter="{StaticResource IPAddressConverter}" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:RegexValidationRule Pattern="^([1-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])(\.([0-9]|[1-9][0-9]|1[0-9][0-9]|2[0-4][0-9]|25[0-5])){3}$" 
                                                            ErrorMessage="Address is required and should look like xxx.xxx.xxx.xxx" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtServerAddress, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="1"/>

                <Label Grid.Column="1"
                               Grid.Row="2">Port</Label>
                <TextBox Grid.Column="2" 
                                 Grid.Row="2" 
                                 Margin="2" 
                                 x:Name="txtPort">
                    <Binding Path="Session.ConnectionInfo.Port" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:IntegerRangeValidationRule MinValue="1" MaxValue="65535" ErrorMessage="Port must be between 1-65535" />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtPort, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="2"/>

                <Label Grid.Column="1" 
                               Grid.Row="3">Username</Label>
                <TextBox x:Name="txtUsername"
                                 Margin="2" 
                                 Grid.Column="2" 
                                 Grid.Row="3">
                    <Binding Path="Session.ConnectionInfo.Username" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:StringRequiredValidationRule ErrorMessage="Username is required." />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtUsername, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="3"/>

                <Label Grid.Column="1" 
                               Grid.Row="4">Password</Label>
                <TextBox Margin="2" 
                                 x:Name="txtPassword" 
                                 Grid.Column="2" 
                                 Grid.Row="4">
                    <Binding Path="Session.ConnectionInfo.ConnectionPassword" UpdateSourceTrigger="Explicit">
                    </Binding>
                </TextBox>


                <Label Grid.Column="1"
                               Grid.Row="5">Full Name</Label>
                <TextBox Grid.Column="2" 
                                 Grid.Row="5" 
                                 Margin="2" 
                                 x:Name="txtFullName">
                    <Binding Path="Session.ConnectionInfo.Realname" UpdateSourceTrigger="Explicit">
                        <Binding.ValidationRules>
                            <data:StringRequiredValidationRule ErrorMessage="Full Name is required." />
                        </Binding.ValidationRules>
                    </Binding>
                </TextBox>
                <ContentPresenter Content="{Binding ElementName=txtFullName, Path=(Validation.Errors).CurrentItem}"
                                          Grid.Column="3"
                                          Grid.Row="5"/>

                <Label Grid.Column="2" 
                               Grid.Row="7" 
                               Margin="2" 
                               Foreground="Red">
                    <Label.Style>
                        <Style TargetType="{x:Type Label}">
                            <Setter Property="Visibility" Value="Collapsed" />
                            <Style.Triggers>
                                <DataTrigger Binding="{Binding Session.State}" Value="ServerError">
                                    <Setter Property="Visibility" Value="Visible" />
                                </DataTrigger>
                            </Style.Triggers>
                        </Style>
                    </Label.Style>
                    Server connection error.
                </Label>
                <Button Grid.Column="2" 
                        Grid.Row="6" 
                        HorizontalAlignment="Right" 
                        Margin="2" 
                        Name="btnConnect" 
                        Width="75" 
                        ToolTip="Connect to server"
                        Command="{x:Static ui:IrcCommands.Login}"
                        CommandParameter="{Binding}"
                        IsDefault="True">
                    _Connect
                </Button>

            </Grid>
        </Grid>
    </DataTemplate>

</ResourceDictionary>